import React, { useState, useEffect } from 'react'
import axios from 'axios'
import { Input } from '@nutui/nutui-react'
import { Button, Space } from 'antd-mobile'
import { useNavigate, useSearchParams } from 'react-router-dom'
export default function Ytype() {
    const [currentTime, setCurrentTime] = useState(getCurrentTime());
    const [mess, setMess] = useState([])
    let navigate = useNavigate()
    let [search] = useSearchParams()
    let id = search.get('id')
    const [val, setVal] = useState('')
    // console.log(id);

    let name = search.get('name')
    let yid = search.get('yid')
    // console.log(typeof yid);
    // 获取当前时间的函数
    function getCurrentTime() {
        let date = new Date();
        let hours = date.getHours();
        let minutes = date.getMinutes();
        minutes = (minutes < 10 ? '0' : '') + minutes;
        return `${hours}:${minutes}`;

    }

    // 使用 useEffect 设置定时器


    useEffect(() => {
        getmsee()
    }, [])
    let top = () => {
        navigate(-1)
    }
    let getmsee = async () => {
        let { data: { data } } = await axios.get('http://127.0.0.1:3000/mxd_getidy', { params: { id, val } })
        // console.log(data);
        setMess(data)
    }
    let cyy = async (id) => {
        // console.log(id);
        let info = mess.find(item => item._id == id)

        info.cy = true
        console.log(info);
        let { data: { data } } = await axios.post('http://127.0.0.1:3000/mxd_upy', info)
        // console.log(data);
        navigate('/gg/Ks')


    }
    let sou = (e) => {
        console.log(e);

        // console.log(val);
        getmsee()
    }
    let addinfoy=async(item)=>{
        // console.log(item);
        let obj={
            name:item.name,
            price:item.price,
            shape:item.shape,
            sym:item.sym,
            grade:item.grade,
            usage:item.usage,
            img:item.img,
            nums:item.nums,
            yid:yid  
        }
        console.log(obj);
        let {data}=await axios.post('http://127.0.0.1:3000/mxd_addcfy',obj)
        // console.log(data);/
        
    }
    return (
        <div id='ytype'>

            <div className="top">
                <p>
                    <span>
                        <svg onClick={top} style={{ width: '30px', height: '35px', position: 'absolute', marginLeft: '5px' }} t="1724675809513" className="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2590" width="200" height="200"><path d="M670.6 861.6c-7.6 0-15.1-2.9-20.9-8.6L329.4 532.7c-11.6-11.5-11.6-30.3 0-41.8l320.2-320.2c5.8-5.8 13.3-8.6 20.9-8.6 7.5 0 15.1 2.9 20.9 8.6 11.5 11.5 11.5 30.2 0 41.8L392 511.8l299.4 299.4c11.5 11.6 11.5 30.3 0 41.8-5.7 5.7-13.3 8.6-20.8 8.6z" fill="#2c2c2c" p-id="2591"></path></svg>
                    </span>
                    <span style={{ marginLeft: '35px', marginTop: '10px', lineHeight: '33px', fontSize: '20px' }}>{name}</span>
                    {/* <span onClick={update} style={{ height: '35px', position: 'absolute', marginLeft: '180px', top: '50px', fontSize: '15px', color: '#52BDFF' }}>编辑处方</span> */}
                </p>
                <hr />
            </div>
            <>
                <Input onKeyDown={(e) => { sou(e) }} onChange={(e) => { setVal(e) }} clearable placeholder='请输入药品名称' />
            </>
            <div className="bigy">
                {mess.filter(item => item.cy === false).map(item => (
                    <div key={item._id} className="smly">
                        <div onClick={() => { navigate('/gg/self?id=' + item._id) }} className="imgs">
                            <img src={item.img} alt="" />
                        </div>
                        <div className="txt">
                            <p style={{ marginTop: '10px', marginLeft: '45px' }}>{item.name}</p>
                            <p style={{ marginTop: '10px', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>规格：{item.grade}</p>
                            <p style={{ marginTop: '10px' }}>辉瑞制药有限公司</p>
                            {yid == 'null' ?
                                <>
                                    <Button onClick={() => { cyy(item._id) }} className='btn1' size='mini' color='primary' fill='solid'>
                                        加入常用药
                                    </Button></>
                                :
                                <>
                                    <Button onClick={() => { addinfoy(item) }} style={{ marginLeft: '120px', marginTop: '10px' }} className='add' size='mini' color='primary' fill='solid'>
                                        添加
                                    </Button>
                                </>
                            }





                            <Button className='btn2' size='mini' shape='rounded' style={{ backgroundColor: '#DA2222', color: 'white' }} fill='solid'>
                                Rx
                            </Button>
                        </div>
                    </div>
                ))}

            </div>
        </div>
    )
}
